package com.df.excel_export.export_java_demo;

import com.df.excel_export.export_java_demo.equip.Tb_Equip;
import com.df.excel_export.export_java_demo.equip.Tb_EquipContainer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

/**
 * 所有横表管理类
 * 1.加容器
 * 2.加解码器
 * 3.加回调
 * 4.此类代码生成
 */
public class TbRoot {
    /**
     * 所有表的管理容器{容器类全限定名:容器对象}
     */
    private static ConcurrentHashMap<String,TbBaseContainer> containers = new ConcurrentHashMap<>();
    /**
     * 表名和容器映射{数据表名:容器对象}
     */
//    private static ConcurrentHashMap<String,TbBaseContainer> containers = new ConcurrentHashMap<>();


    /**
     * 表数据重新加载后的回调,用于监听别的表数据加载{}
     * 先所有单表加载，然后回调执行，注意多条件回调情况(一个表要其他几个表全部加载后才执行的回调)
     * 一般是对自己表的数据再组装，但是会依赖到其他表数据
     */
    private static ConcurrentHashMap<String, Set<Consumer<String>>> dataLoadedCb = new ConcurrentHashMap<>();


    public static void registerContainer(TbBaseContainer container){
        if(containers.containsKey(container.getClass().getName())){
            System.out.println("尝试重复注册容器："+container.getClass().getName());
            return;
        }
        containers.put(container.getClass().getName(),container);
    }

    public static <T,C extends TbBaseContainer<T>>  C  getContainer(Class<C> clazz){
        TbBaseContainer<T> tbBaseContainer = containers.get(clazz.getName());
        if(tbBaseContainer==null){
            return null;
        }
        return (C)tbBaseContainer;
    }

    public static <T,C extends TbBaseContainer<T>> T  getCfgByid(Class<C> containerClazz,Integer id){
        TbBaseContainer<T> tbBaseContainer = containers.get(containerClazz.getName());
        if(tbBaseContainer==null){
            return null;
        }
        T cfgById = tbBaseContainer.getCfgById(id);
        if(cfgById==null){
            return null;
        }
        return cfgById;
    }
    public static <T,C extends TbBaseContainer<T>> List<T>  getCfgs(Class<C> containerClazz){
        TbBaseContainer<T> tbBaseContainer = containers.get(containerClazz.getName());
        if(tbBaseContainer==null){
            return null;
        }
        List<T> cfgById = tbBaseContainer.getCfgs();
        if(cfgById==null){
            return null;
        }
        return cfgById;
    }

    public static <C extends TbBaseContainer> Integer  getMaxId(Class<C> containerClazz){
        TbBaseContainer tbBaseContainer = containers.get(containerClazz.getName());
        if(tbBaseContainer==null){
            return null;
        }
        return  tbBaseContainer.getMaxId();
    }

    public static <C extends TbBaseContainer> Integer  getMinId(Class<C> containerClazz){
        TbBaseContainer tbBaseContainer = containers.get(containerClazz.getName());
        if(tbBaseContainer==null){
            return null;
        }
        return  tbBaseContainer.getMinId();
    }

    public static void reloadTbData(List<String> dataNames){
        //TODO 单表加载可以使用线程池
        try {
            if(dataNames!=null && !dataNames.isEmpty()){
                for (String dataName : dataNames) {
                    for (TbBaseContainer value : containers.values()) {
                        if(value.getTbName().equals(dataName)){
                            value.loadData();
                        }
                    }
                }

            }else {
                for (TbBaseContainer value : containers.values()) {
                    value.loadData();
                }
            }
            //TODO 发布解析完成的事件
            //TODO 解码后检查回调条件是否满足，单条件，多条件
//            Set<Consumer<String>> stringConsumer = dataLoadedCb.get(dataName);
//            if(stringConsumer!=null && !stringConsumer.isEmpty()){
//                for (Consumer<String> consumer : stringConsumer) {
//                    consumer.accept(dataName);
//                }
//            }
        }catch (Exception e){
            e.printStackTrace();
        }

        //调用读取完成后的函数
    }

    //加载全部数据
    public static void reloadTbDataAll(){
        reloadTbData(null);
    }

    /**
     * 扫描指定包下的container实现类
     */
    public static void scanContainers(String ... packages){

    }


    public static void main(String[] args) {
        //添加解码器和容器
        //TODO 从容器中获取
        TbRoot.scanContainers();
        //加载数据
        TbRoot.reloadTbDataAll();
        //使用数据
        Tb_EquipContainer container1 = TbRoot.getContainer(Tb_EquipContainer.class);

        Tb_Equip cfgByid = TbRoot.getCfgByid(Tb_EquipContainer.class, 1);
        List<Tb_Equip> cfgs = TbRoot.getCfgs(Tb_EquipContainer.class);
    }

    public static void listenTbChange(Consumer cb,String... names) {


    }
}
